package com.situ.mall.controller;


import com.situ.mall.pojo.Product;
import com.situ.mall.pojo.query.ProductQuery;
import com.situ.mall.service.IProductService;
import com.situ.mall.util.PageResult;
import com.situ.mall.util.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Indexed;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Gao
 * @since 2025-06-17
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;

    //导入
    @RequestMapping("/importExcel")
    public Result importExcel(MultipartFile file) {
        productService.importExcel(file);
        return Result.ok("上传成功");
    }

    //导出
    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) {
        productService.exportExcel(response);
    }

    @RequestMapping("/update")
    public Result update(Product product) {
        productService.updateById(product);
        return Result.ok("修改成功");
    }

    @RequestMapping("/selectById")
    public Result<Product> selectById(Integer id) {
        Product product = productService.getById(id);
        return Result.ok(product);
    }

    @RequestMapping("/add")
    public Result add(Product product) {
        productService.save(product);
        return Result.ok("添加成功");
    }

    // /product/list?page=1&limit=10
    // /product/list?page=1&limit=10&name=zhangsan
    // /product/list?page=1&limit=10&name=zhangsan&beginCreateDate
    @RequestMapping("/list")
    public PageResult<List<Product>> list(ProductQuery productQuery) {
        PageResult<List<Product>> pageResult = productService.list(productQuery);
        return pageResult;
    }

    @RequestMapping("/deleteById")
    public Result deleteById(Integer id) {
        productService.removeById(id);
        return Result.ok("删除成功");
    }

    @RequestMapping("/deleteAll")
    public Result deleteAll(Integer[] ids) {
        System.out.println(Arrays.toString(ids));
        productService.removeBatchByIds(Arrays.asList(ids));
        return Result.ok("删除成功");
    }


    /*@RequestMapping("/toProductList")
    public String toProductList() {
        return "product_list";
    }*/
}

